package jps_s10996_s7175;

import jps_s10996_s7175.result.BagResult;
import jps_s10996_s7175.result.BinderResult;
import jps_s10996_s7175.result.BooleanResult;
import jps_s10996_s7175.result.IntegerResult;
import jps_s10996_s7175.result.QResStack;
import jps_s10996_s7175.result.StringResult;
import jps_s10996_s7175.result.StructResult;
import edu.pjwstk.jps.result.ISingleResult;

public class QRES {

	public static void cw2_s3() {
		QResStack qres = new QResStack();

		qres.push(new IntegerResult(1));
		qres.push(new IntegerResult(2));

		BagResult op1_res = new BagResult((IntegerResult) qres.pop(),
				(IntegerResult) qres.pop());

		qres.push(op1_res);
		qres.push(new StringResult("x"));

		BinderResult op2_res = new BinderResult(
				((StringResult) qres.pop()).getValue(), (BagResult) qres.pop());

		qres.push(op2_res);
		qres.push(new IntegerResult(3));
		qres.push(new IntegerResult(4));

		IntegerResult op3_right = (IntegerResult) qres.pop();
		IntegerResult op3_left = (IntegerResult) qres.pop();

		BagResult op3_res = new BagResult(op3_left, op3_right);

		qres.push(op3_res);
		qres.push(new IntegerResult(5));

		IntegerResult op4_right = (IntegerResult) qres.pop();
		BagResult op4_res = new BagResult();

		for (ISingleResult er1 : ((BagResult) qres.pop()).getElements()) {
			op4_res.add(new StructResult(er1, op4_right));
		}

		qres.push(op4_res);

		// x , bag
		BagResult op5_res = new BagResult();
		BagResult op5_right = (BagResult) qres.pop();
		BinderResult op5_left = (BinderResult) qres.pop();

		for (ISingleResult er1 : op5_right.getElements()) {
			op5_res.add(new StructResult(op5_left, er1));
		}

		System.out.println("(((bag(1, 2) groupas x), bag(3, 4), 5");
		System.out.println("\tprint result:");
		System.out.println("\t" + op5_res.stringme() + "\n");
	}

	public static void cw2_s2() {
		QResStack qres = new QResStack();

		qres.push(new StringResult("ala"));
		qres.push(new StringResult(" ma"));

		StringResult op_1_right = (StringResult) qres.pop();
		StringResult op_1_left = (StringResult) qres.pop();

		StringResult op_1_res = new StringResult(op_1_left.getValue()
				+ op_1_right.getValue());
		qres.push(op_1_res);

		// +
		qres.push(new StringResult(" kota"));

		StringResult op_2_right = (StringResult) qres.pop();
		StringResult op_2_left = (StringResult) qres.pop();

		StringResult op_2_res = new StringResult(op_2_left.getValue()
				+ op_2_right.getValue());
		qres.push(op_2_res);

		// bag

		BagResult op_4_res = new BagResult((StringResult) qres.pop());
		qres.push(op_4_res);
		// *

		qres.push(new IntegerResult(8));
		qres.push(new IntegerResult(10));

		IntegerResult op_5_left = (IntegerResult) qres.pop();
		IntegerResult op_5_right = (IntegerResult) qres.pop();

		IntegerResult op_5_res = new IntegerResult(op_5_left.getValue()
				* op_5_right.getValue());
		qres.push(op_5_res);

		// bag
		qres.push(new BooleanResult(false));

		BooleanResult op_6_right = (BooleanResult) qres.pop();
		IntegerResult op_6_left = (IntegerResult) qres.pop();

		// foreach elem in result put it into bag
		BagResult op_6_res = new BagResult(op_6_left, op_6_right);
		qres.push(op_6_res);

		// bag1,bag2
		// foreach in bag 1
		// foreach in bag 2
		// connect
		BagResult finalResult = new BagResult();
		BagResult op_7_right = (BagResult) qres.pop();
		BagResult op_7_left = (BagResult) qres.pop();

		for (ISingleResult er1 : op_7_left.getElements()) {
			for (ISingleResult er2 : op_7_right.getElements()) {
				finalResult.add(new StructResult(er1, er2));
			}
		}

		System.out
				.println("(bag(\"ala\"+\" ma\"+\" kota\"), bag(8*10, false));");
		System.out.println("\tprint result:");
		System.out.println("\t" + finalResult.stringme() + "\n");
	}

	public static void cw2_s1() {
		QResStack qres = new QResStack();

		qres.push(new IntegerResult(1));
		qres.push(new IntegerResult(2));
		qres.push(new IntegerResult(1));

		// 2+1
		IntegerResult sumLeft = (IntegerResult) qres.pop(); // 1
		IntegerResult sumrRight = (IntegerResult) qres.pop(); // 2
		IntegerResult sumRes = new IntegerResult(sumLeft.getValue()
				+ sumrRight.getValue());
		qres.push(sumRes);

		// struct (1 , 2+1)
		IntegerResult struLeft = (IntegerResult) qres.pop(); // 1
		IntegerResult struRight = (IntegerResult) qres.pop(); // 1
		// comma expression
		StructResult struRes = new StructResult(struLeft, struRight);
		qres.push(struRes);
		// System.out.println(struRes.stringme());

		// begin cartesian ,
		// lowest node
		qres.push(new StringResult("test"));
		qres.push(new StringResult("Ala"));

		StringResult stru2Right = (StringResult) qres.pop();
		StringResult stru2Left = (StringResult) qres.pop();
		// comma expression
		// StructResult stru2Res = new StructResult(stru2Left, stru2Right);
		// qres.push(stru2Res);
		// bag
		// treat it as a structure and add each element

		// StructResult coll = (StructResult) qres.pop();
		BagResult bag = new BagResult();
		bag.add(stru2Left);
		bag.add(stru2Right);
		// for ( ISingleResult res : coll.elements()) {
		// bag.add(res);
		// }

		qres.push(bag);

		// start right node of "as" expression
		qres.push(new StringResult("nazwa"));

		StringResult rightOperator = (StringResult) qres.pop();
		BagResult leftOperator = (BagResult) qres.pop();

		BinderResult bindResult = new BinderResult(rightOperator.getValue(),
				leftOperator);
		qres.push(bindResult);

		// top-node : comma opreator
		ISingleResult topRight = (ISingleResult) qres.pop();
		ISingleResult topLeft = (ISingleResult) qres.pop();
		StructResult finalResult = new StructResult(topLeft, topRight);

		qres.push(finalResult);

		System.out
				.println("(struct(1, 2+1), (bag(\"test\", \"Ala\") as nazwa))");
		System.out.println("\tprint result:");
		System.out.println("\t" + finalResult.stringme() + "\n");
	}

}
